package com.ls.blog.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.ls.blog.entity.SysUser;

import java.util.Date;

/**
 * @program: myblog
 * @author: lishun
 * @create: 2021-07-07 16:28
 **/
public class TokenUtils {

    /**
     * token的生成方法
     * @param user
     * @return
     */
    public static String createToken(SysUser user) {
        String token="";
        // 使用用户的密码当作密钥
        token= JWT.create()
                .withAudience(String.valueOf(user.getId()))
                .withIssuedAt(new Date())
                .withExpiresAt(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 60 * 1000))
                .sign(Algorithm.HMAC256(user.getPassword()));
        return token;
    }
}
